import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import { createAction, NavigationActions } from '../../utils'
import * as Libs from '../../utils/Libs'
import * as statisticsService from '../../services/statistics/statisticsService'

const initState = {
  mainTime: {
    startTime: Libs.getFirstMonth(),
    endTime: Libs.getCurrentMonth(),
    timeType: 'year'
  },
  zichanGS: "",
  lirunGS: "",
  xjllGS: "",
  ylTable :[
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
  ],
  czTable: [
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
  ],
  yyTable: [
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
  ],
  czqkTable: [
    ['', '', ''],
    ['', '', ''],
    ['', '', ''],
  ],
  ylDetail: "",
  czDetail: "",
  yyDetail: "",
  czqkDetail: "",
  problemReportContent: []
}

export default {
  namespace: 'statistics',
  state: initState,
  reducers: {
    changeTime(state, { payload }) {
      return { ...state, ...payload}
    },
    initState(state, { payload }) {
      return initState
    },
    changeState(state, { payload }) {
      return { ...state, ...payload}
    }
  },
  effects: {
    *getTmodeContentMsg({ payload }, { call, select, put}) {
      console.log('main msg payload is ', payload)
      const reportType = payload.reportType
      let params = _.clone(payload)
      params.startTime = (payload.startTime || "").replace('.', '-')
      params.endTime = (payload.endTime || "").replace('.', '-')
      const resp = yield call(statisticsService.getMainPage, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data || {}
      const zichanGS = (respD.fzList || [])[0] || ""
      const lirunGS = (respD.lrList || [])[0] || ""
      const xjllGS = (respD.xjList || [])[0] || ""

      //盈利能力
      let ylTable = []
      const ylnlD = respD.ylnl || {}
      ylTable.push(statisticsService.buildArrayD(ylnlD.yysr))
      ylTable.push(statisticsService.buildArrayD(ylnlD.yycb))
      ylTable.push(statisticsService.buildArrayD(ylnlD.ml))
      ylTable.push(statisticsService.buildArrayD(ylnlD.mllv))
      ylTable.push(statisticsService.buildArrayD(ylnlD.jlr))
      ylTable.push(statisticsService.buildArrayD(ylnlD.jlrlv))
      const ylDetail = (ylnlD.ylContent || [])[0] || ""

      //偿债能力
      let czTable = []
      const cznlD = respD.cznl || {}
      czTable.push(statisticsService.buildArrayD(cznlD.zczj))
      czTable.push(statisticsService.buildArrayD(cznlD.fzzj))
      czTable.push(statisticsService.buildArrayD(cznlD.jzczj))
      czTable.push(statisticsService.buildArrayD(cznlD.zcfzl))
      czTable.push(statisticsService.buildArrayD(cznlD.ldblv))
      // czTable.push(statisticsService.buildArrayD(cznlD.lxbs))
      const czDetail = (cznlD.czContent || [])[0] || ""

      //营运情况
      let yyTable = []
      const yyqkD = respD.yyqk || {}
      yyTable.push(statisticsService.buildArrayD(yyqkD.jyhdxjl))
      yyTable.push(statisticsService.buildArrayD(yyqkD.ysz))
      yyTable.push(statisticsService.buildArrayD(yyqkD.chzl))
      const yyDetail = (yyqkD.yyunContent || [])[0] || ""

      //成长情况
      let czqkTable = []
      const czqkD = respD.czqk || {}
      czqkTable.push(statisticsService.buildArrayD(czqkD.zzczz))
      czqkTable.push(statisticsService.buildArrayD(czqkD.yysrzz))
      czqkTable.push(statisticsService.buildArrayD(czqkD.lrlzz))
      const czqkDetail = (czqkD.czContent || [])[0] || ""

      yield put(createAction("changeState")({
        zichanGS: zichanGS,
        lirunGS: lirunGS,
        xjllGS: xjllGS,
        ylTable: ylTable,
        czTable: czTable,
        yyTable: yyTable,
        czqkTable: czqkTable,
        ylDetail: ylDetail,
        czDetail: czDetail,
        yyDetail: yyDetail,
        czqkDetail: czqkDetail
      }))
    },
    *getProblemReport({payload}, {call, select, put}) {
      let params = _.clone(payload)
      params.startTime = (payload.startTime || "").replace('.', '-')
      params.endTime = (payload.endTime || "").replace('.', '-')
      const resp = yield call(statisticsService.getProblemReport, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data
      let fullArr = []
      _.map(respD, (v) => {
        let arr = []
        arr.push(v.codeName)
        arr.push(v.sumMoney)
        arr.push(v.tbSumMoney)
        arr.push(v.onRise)
        fullArr.push(arr)
      })
      yield put(createAction("changeState")({
        problemReportContent: fullArr
      }))
    }
  },
}
